﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

namespace mpsalaryprojectgroup
{
    public partial class NEWForgetPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            Button1.Visible = false;
            DisableBufferingOnPage();
        }
        protected void DisableBufferingOnPage()
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            // set expiry date in the past
            Response.Expires = -1;
            Response.CacheControl = "no-cache";
            Response.Cache.SetNoStore();
            Response.AppendHeader("Pragma", "no-cache");
            Response.AppendHeader("Cache-Control", "no-cache");
            Response.CacheControl = "no-cache";
            Response.Expires = -1;
            Response.ExpiresAbsolute = new DateTime(1900, 1, 1);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }
        protected void btnforgetPassword_Click(object sender, EventArgs e)
        {
         
            mpsalary.App_Code.User u = new mpsalary.App_Code.User();
            u.CheckEmail();

            if (u.Email == tbxemail.Text)
            {
                u.RetrieveForgetPassword();
                string u1 = u.Usename;
                string p1 = u.Password;


                try
                {

                    MailMessage email = new MailMessage();
                    email.From = new MailAddress("Fromthepgsteam@gmail.com", "PGS Portal");
                    email.To.Add(new MailAddress(tbxemail.Text));
                    email.Subject = "PGS Portal- Forget your password?";
                    email.Body = "<html><body><b>Dear " + u1 + ",</b><br><br>Here's the PGS Portal information you have requested - be sure to keep it in a safe place.<br><br>Username: " + u1 + "<br>Password:  " + p1 + "<br><br><br><b>Yours sincerely,<br> The PGS Team<br><br></b> </body></html>";
                    email.IsBodyHtml = true;
                    email.Priority = MailPriority.Normal;

                    SmtpClient mailclient = new SmtpClient("smtp.gmail.com");
                    mailclient.Credentials = new System.Net.NetworkCredential("thepgsteam@gmail.com", "thepgsteam");
                    mailclient.Port = 587;
                    mailclient.EnableSsl = true;
                    mailclient.Send(email);

                    lblemail.Text = "Email sent";
                }
                catch
                {
                    lblemail.Text = "Sending of mail failed, Try again?";
                }
            }

            else
            {
                lblemail.Text = "Email does not exist";

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Panelforgetpassword.Visible = true;
          
            tbxemail.Text = "";
            lblemail.Text = "";

        }

        

    }
}